// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
	"category_id": {
		"rules": [{
			"format": "string"
		}]
	},
	"name": {
		"rules": [{
			"format": "string"
		}],
		"title": "名称",
		"label": "名称"
	},
	"crossed_price": {
		"rules": [{
			"format": "int"
		}]
	},
	"goods_services": {
		"rules": [{
			"format": "array"
		}]
	},
	"freight": {
		"rules": [{
			"format": "bool"
		}]
	},
	"freight_amount": {
		"rules": [{
			"format": "int"
		}]
	},
	"goods_desc": {
		"rules": [{
			"format": "string"
		}],
		"title": "详细描述",
		"label": "详细描述"
	},
	"goods_thumb": {
		"rules": [{
				"format": "string"
			},
			{
				"pattern": "^(http://|https://|/|./|@/)\\S"
			}
		],
		"title": "缩略图地址",
		"label": "缩略图地址"
	},
	"goods_banner_imgs": {
		"rules": [{
				"format": "array"
			},
			{
				"arrayType": "file"
			},
			{
				"maxLength": 5
			}
		]
	},
	"videos": {
		"rules": [{
				"format": "array"
			},
			{
				"arrayType": "file"
			},
			{
				"maxLength": 1
			}
		],
		"title": "商品详情页的banner视频",
		"label": "商品详情页的banner视频"
	},
	"total_sell_count": {
		"rules": [{
			"format": "int"
		}]
	},
	"remain_count": {
		"rules": [{
			"format": "int"
		}],
		"title": "库存数量",
		"label": "库存数量"
	},
	"comment_count": {
		"rules": [{
			"format": "int"
		}]
	},
	"is_on_sale": {
		"rules": [{
			"format": "bool"
		}],
		"title": "是否上架",
		"label": "是否上架"
	},
	"is_best": {
		"rules": [{
			"format": "bool"
		}]
	},
	"is_new": {
		"rules": [{
			"format": "bool"
		}],
		"title": "是否参与秒杀",
		"label": "是否参与秒杀"
	},
	"is_hot": {
		"rules": [{
			"format": "bool"
		}]
	},
	"suks": {
		"rules": [{
			"format": "array"
		}]
	},
	"orig_suks": {
		"rules": [{
			"format": "array"
		}]
	},
	"fs_suks": {
		"rules": [{
			"format": "array"
		}]
	},
	"orig_fs_suks": {
		"rules": [{
			"format": "array"
		}]
	}
}

const enumConverter = {}

function filterToWhere(filter, command) {
	let where = {}
	for (let field in filter) {
		let {
			type,
			value
		} = filter[field]
		switch (type) {
			case "search":
				if (typeof value === 'string' && value.length) {
					where[field] = new RegExp(value)
				}
				break;
			case "select":
				if (value.length) {
					let selectValue = []
					for (let s of value) {
						selectValue.push(command.eq(s))
					}
					where[field] = command.or(selectValue)
				}
				break;
			case "range":
				if (value.length) {
					let gt = value[0]
					let lt = value[1]
					where[field] = command.and([command.gte(gt), command.lte(lt)])
				}
				break;
			case "date":
				if (value.length) {
					let [s, e] = value
					let startDate = new Date(s)
					let endDate = new Date(e)
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
			case "timestamp":
				if (value.length) {
					let [startDate, endDate] = value
					where[field] = command.and([command.gte(startDate), command.lte(endDate)])
				}
				break;
		}
	}
	return where
}

export {
	validator,
	enumConverter,
	filterToWhere
}